package com.lsz.intelligentflowerwatering.ui

import android.content.Context
import android.content.DialogInterface
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.lsz.intelligentflowerwatering.ui.theme.MyComposeTheme

const val tag = "the process"

@Composable
fun TheButton(text: String = "undefined", onClick: () -> Unit = { }) {
    Button(
        onClick = onClick,
        colors = ButtonDefaults.buttonColors(
            backgroundColor = MyComposeTheme.colors.buttonBackground,
            contentColor = MyComposeTheme.colors.buttonContent
        ),
        shape = RoundedCornerShape(10.dp)
    ) {
        Text(text = text, fontSize = 20.sp)
    }
}

@Composable
fun TheText(text: String) {
    Text(
        text = text,
        Modifier.padding(vertical = 10.dp, horizontal = 20.dp),
        fontSize = 15.sp
    )
}

@Preview
@Composable
fun TheSettingItem(isFinished: Boolean = false, text: String = "正在配置") {
    Row(
        verticalAlignment = Alignment.CenterVertically, modifier = Modifier
            .height(60.dp)
    ) {
        if (isFinished) {
            Icon(
                Icons.Filled.Check,
                "check",
                tint = Color.Green,
                modifier = Modifier.size(30.dp)
            )
        } else {
            CircularProgressIndicator(
                color = Color.Gray,
                strokeWidth = 3.dp,
                modifier = Modifier
                    .size(30.dp)
                    .padding(5.dp)
            )
        }
        Text(text = text, fontSize = 25.sp, modifier = Modifier.padding(start = 20.dp), color = MyComposeTheme.colors.dialog)
    }
}

fun dialog(
    context: Context,
    message: String = "",
    positiveButton: String = "是",
    positiveButtonClicked: (DialogInterface, Int) -> Unit = {_,_->},
    negativeButton: String = "否",
    negativeButtonClicked: (DialogInterface, Int) -> Unit = {_,_->}
) {
    Log.i(tag, "dialog")
    val builder = android.app.AlertDialog.Builder(context)
    builder.setMessage(message)
    builder.setPositiveButton(positiveButton, positiveButtonClicked)
    builder.setNegativeButton(negativeButton, negativeButtonClicked)
    builder.show()
}

fun toast(
    context: Context ,
    text: String = "",
    time: Int = Toast.LENGTH_LONG
) {
    Toast.makeText(context, text, time).show()
}


